<script setup>
import { ref, watch, onMounted } from 'vue'
import { getExpertPageAPI } from '@/apis/expert'
import { getHotQAAPI } from '@/apis/question'

const expertList = ref([])
const qaLsit = ref([])
const page = ref({
    current: 1,
    pageSize: 10,
    total: 100
})

watch(
    () => page.value.current,
    (newVal) => {
        getPage(newVal)
    }
)

const getPage = async (pageNum) => {
    const res = await getExpertPageAPI({ page: pageNum })
    // console.log(res)
    if (res.code === 2000) {
        expertList.value = res.data.records
        page.value.total = res.data.total
    }
}

onMounted(async () => {
    getPage(1)
    const res = await getHotQAAPI()
    // console.log(res)
    if (res.code === 2000) {
        qaLsit.value = res.data
    }
})

let sortType = [
    "默认",
    "被提问量",
    "有用量",
    "回复率",
]
let categoryList = [
    "全部", "禽畜养殖", "水果种植", "种子种苗",
    "蔬菜种植", "农机", "苗木花草种植", "中药材种植",
    "水产养殖", "经济作物种植", "蔬菜储藏加工", "水果储藏加工", "农资"
]
</script>

<template>
<div class="guide-page clearfix">
    <div class="page-l fl">
        <div class="page-header">
            <div class="category page-header-box">
                <div class="box-title">排序方式</div>
                <div class="box-content">
                    <a href="#" v-for="(type, i) in sortType" :key="i" :class="{active: i === 0}">{{ type }}</a>
                </div>
            </div>
            <div class="sort page-header-box">
                <div class="box-title">分类</div>
                <div class="box-content">
                    <a href="#" v-for="(category, i) in categoryList" :key="i" :class="{active: i === 0}">{{ category }}</a>
                </div>
            </div>
        </div>
        <div class="guide-list">
            <div class="guide-card" v-for="expert in expertList" :key="expert.id">
                <div class="card-l"><img src="@/assets/icon/expert.png" alt=""></div>
                <div class="card-r">
                    <div class="card-r-top">
                        <div class="user">{{ expert.realName }}·{{ expert.position }}</div>
                        <div class="info">
                            <div>联系电话：{{ expert.phone }}</div>
                            <div>所属院系：{{ expert.belong }}</div>
                        </div>
                        <div class="skill">擅长：猪，牛，羊，鸡，鸭，鹅等畜牧的疾病防控技术</div>
                    </div>
                    <div class="card-r-bottom">
                        <RouterLink :to="`/ask/${expert.id}`" class="card-btn">免费提问</RouterLink>
                    </div>
                </div>
            </div>
        </div>
        <div class="pagination">
            <el-pagination 
                background 
                layout="prev, pager, next" 
                v-model:page-size="page.pageSize"
                v-model:current-page="page.current"
                :total="page.total" 
            />
        </div>
    </div>
    <div class="page-r fr">
        <div class="page-r-title">
            热门问答
            <img src="@/assets/icon/hot.png" alt="">
        </div>
        <div class="qa-list">
            <div class="qa-card" v-for="qa in qaLsit" :key="qa.id">
                <a href="javascript:">
                    <div class="ask">
                        <div class="icon"><img src="@/assets/icon/ask.png" alt=""></div>
                        <div class="content">{{ qa.content }}</div>
                    </div>
                    <div class="answer">
                        <div class="icon"><img src="@/assets/icon/ac80113.png" alt=""></div>
                        <div class="content">{{ qa.answer?.content }}</div>
                    </div>
                </a>
            </div>
        </div>
    </div>
</div>
</template>

<style scoped lang='scss'>
.guide-page {
    width: 1280px;
    margin: 20px auto;

    .page-l {
        width: 860px;
        padding: 20px;
        background-color: #fff;

        .page-header {
            width: 100%;

            .page-header-box {
                display: flex;
                width: 100%;
                min-height: 40px;
                margin-bottom: 20px;
                border-bottom: 1px dashed #d8d8d8;

                .box-title {
                    height: 100%;
                    width: 90px;
                    padding-top: 3px;
                }

                .box-content {
                    width: 720px;
                    display: flex;
                    flex-wrap: wrap;
                    
                    >a {
                        font-size: 14px;
                        display: block;
                        padding: 5px 20px;
                        margin-bottom: 10px;

                        &:hover {
                            color: #39bf3e;
                        }
                    }

                    .active {
                        color: #fff;
                        background-color: #39bf3e;

                        &:hover {
                            color: #fff;
                        }
                    }
                }
            }
        }

        .guide-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            width: 100%;
            min-height: 400px;

            .guide-card {
                display: flex;
                width: 400px;
                height: 170px;
                padding: 10px 20px;
                margin-bottom: 20px;
                background-color: #f5f5f5;
                transition: all .3s;

                &:hover {
                    box-shadow: 0 2px 12px 0 rgba(0,133,25,.12);
                    background-color: #fff;
                }

                .card-l {
                    width: 100px;
                    height: 100%;

                    img {
                        width: 80px;
                        height: 80px;
                    }
                }

                .card-r {
                    width: 260px;
                    height: 100%;

                    .card-r-top {
                        display: flex;
                        flex-direction: column;
                        justify-content: space-around;
                        width: 100%;
                        height: 110px;

                        .info {
                            font-size: 12px;
                        }

                        .skill {
                            width: 100%;
                            height: 38px;
                            font-size: 14px;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            display: -webkit-box;
                            -webkit-line-clamp: 2;
                            -webkit-box-orient: vertical;
                        }
                    }

                    .card-r-bottom {
                        width: 100%;
                        height: 40px;
                        padding: 5px 0;

                        .card-btn {
                            display: block;
                            height: 30px;
                            width: 150px;
                            line-height: 28px;
                            color: #39bf3e;
                            font-size: 14px;
                            border-radius: 30px;
                            border: 1px solid #39bf3e;
                            text-align: center;

                            &:hover {
                                color: #fff;
                                background-color: #39bf3e;
                            }
                        }
                    }
                }
            }
        }

        .pagination {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 60px;
        }
    }

    .page-r {
        width: 400px;
        padding: 20px;
        background-color: #fff;

        .page-r-title {
            height: 40px;
            width: 100%;
            margin-bottom: 20px;
            font-size: 18px;
            font-weight: bold;
            border-bottom: 1px solid #d8d8d8;

            img {
                width: 20px;
                height: 20px;
                margin-top: -3px;
            }
        }

        .qa-list {
            width: 100%;
            min-height: 400px;
        }

        .qa-card {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            background-color: #f5f5f5;
            transition: all .3s;

            &:hover {
                box-shadow: 0 2px 12px 0 rgba(0,133,25,.12);
                background-color: #fff;
            }

            >a {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }

            .ask, .answer {
                display: flex;
                justify-content: space-between;
                font-size: 14px;
                width: 100%;
                margin-bottom: 10px;
            }

            .ask {
                font-weight: bold;
            }

            .icon {
                width: 20px;
                height: 20px;
            }

            .content {
                width: 310px;
            }
        }
    }
}
</style>
